package com.ciaojian.core.util;

import com.ciaojian.core.enums.sys.StatusCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author Atlakyn
 */
@ApiModel
@Data
public class Result {
    /**
     * 旧的状态码字段,已废弃,请使用 {@link com.ciaojian.core.util.Result#code}
     */
    @Deprecated
    private Integer success;

    @ApiModelProperty("状态码")
    private Integer code;

    @ApiModelProperty("返回信息")
    private String message;

    @ApiModelProperty("返回数据")
    private Object data;

    /**
     * 成功信息常量
     */
    public static final String SUCCESS = "SUCCESS";

    /**
     * 参数错误信息常量
     */
    public static final String PARAMETER_ERROR = "PARAMETER_ERROR";

    public static Result result(Integer code, String message, Object data) {
        return new Result(code, message, data);
    }

    public static Result result(Integer code, String message) {
        return new Result(code, message);
    }

    /**
     * 保存/更新数据结果
     *
     * @param flag 保存/更新数据返回的结果
     * @return 保存/更新返回 200 成功
     */
    public static Result save(boolean flag) {
        if (flag) {
            return new Result(200, "成功!");
        } else {
            return new Result(5001, "失败!");
        }
    }

    /**
     * 保存/更新数据结果
     *
     * @param o 保存/更新数据返回的结果
     * @return 保存/更新返回 200 成功
     */
    public static Result save(Object o) {
        if (o != null) {
            return new Result(200, "成功!", o);
        } else {
            return new Result(5001, "失败!");
        }
    }

    public static Result ok() {
        return ok(200, Result.SUCCESS);
    }

    public static Result ok(Object data) {
        return ok(200, Result.SUCCESS, data);
    }

    public static Result ok(String message, Object object) {
        return ok(200, message, object);
    }

    public static Result ok(int code, String message) {
        return ok(code, message, null);
    }

    public static Result ok(int code, String message, Object data) {
        return new Result(code, message, data);
    }

    public static Result ok(StatusCode code, String message, Object data) {
        return new Result(code.getCode(), message, data);
    }

    /**
     * 失败
     *
     * @return 消息
     */
    public static Result fail() {
        return fail(StatusCode.FAIL);
    }

    public static Result fail(StatusCode code) {
        return new Result(code.getCode(), code.getDescription(), null);
    }

    public static Result fail(StatusCode code, Object data) {
        return new Result(code.getCode(), code.getDescription(), data);
    }

    public static Result fail(StatusCode code, String message, Object data) {
        return new Result(code.getCode(), message, data);
    }

    public static Result error(String message) {
        return error(400, message);
    }

    public static Result error(String message, Object data) {
        return error(400, message);
    }

    public static Result error(int code, String message, Object data) {
        return new Result(code, message, data);
    }

    public static Result error(int code, String message) {
        return new Result(code, message);
    }

    private Result(Integer code, String message) {
        this.code = code;
        this.success = code;
        this.message = message;
    }

    private Result(Integer code, Object data) {
        this.code = code;
        this.success = code;
        this.data = data;
    }

    private Result(Integer code, String message, Object data) {
        this.code = code;
        this.success = code;
        this.data = data;
        this.message = message;
    }
}
